// pages/history/history.js
import {request} from "~/utils/request.js"
import { formatTime } from "~/utils/util.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    totalElements: 0,
    page: 0,
    loading: false,
    triggered:false,
    noMore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('12312312312')
    this.bindscrolltoupper()
  },

  getList () {
    if (this.data.noMore) return
    this.data.loading = true
    let data = {
      page: this.data.page,
      size: 40,
      sort: 'enterTime,desc'
    }
    if (this.data.page == 0) {
      this.data.list = []
    }
    request('member/visit', data).then(res => {
      res.content.map(v =>{
        console.log(v.enterTime)
        v.endTime = formatTime(new Date(v.enterTime))
      })
      this.setData({
        list: [...this.data.list, ...res.content],
        totalElements: res.totalElements,
        loading: false,
        triggered: false,
        noMore: res.totalElements < (this.data.page + 1) * 20
      })
    }).catch(err =>{
      this.setData({
        loading: false,
        triggered: false
      })
    })
  },
  onClick (e) {
    wx.setStorageSync('enterDetail', e.currentTarget.dataset.detail)
    wx.navigateTo({
      url: '/pages/detail/detail',
    })
  },
  bindscrolltolower (e) {
    if (this.data.loading) return
    this.data.page++
    this.getList()
  },
  bindscrolltoupper () {
    this.setData({
      triggered: true,
      page: 0,
      noMore: false
    })
    this.getList()
  }
})